package com.hongdee.scm.inventory.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.inventory.service.ITScmInventoryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmInventoryController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.inventory.controller
 * 作  者: 术洪超
 * 日  期:2017-06-01 14:45
 */
@RestController
@RequestMapping("/scm/inventoryDetail")
public class TScmInventoryDetailController {

    @Autowired
    private ITScmInventoryDetailService itScmInventoryDetailService;

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择任务！");
        }
        Map map = itScmInventoryDetailService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "id", defaultValue = "") String id,
                    @RequestParam(name = "vin", defaultValue = "") String vin,
                    @RequestParam(name = "carModel", defaultValue = "") String carModel) throws Exception {

        Map map = itScmInventoryDetailService.page(start, limit, vin, carModel, id, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * 保存盘点信息明细
     *
     * @param uid
     * @param token
     * @param data
     * @throws Exception
     */
    @RequestMapping("/saveInventoryDetail")
    @ResponseBody
    public ResultEntity saveInvoiceDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token,
                                          @RequestParam(name = "id", defaultValue = "") String id,
                                          @RequestBody List<Map> data) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmInventoryDetailService.saveOrUpdateDetail(data, uid, id);

        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }



    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param vin    vin数组
     * @return ResultEntity
     * @throws Exception
     * @desc 打印入库单
     * @author 术洪超
     */

    @RequestMapping("/printInventoryDetail")
    @ResponseBody
    public ResultEntity printValidate(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token,
                                      @RequestParam(name = "id", defaultValue = "") String id,
                                      @RequestBody String[] vin) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmInventoryDetailService.updateValidateStatus(vin,uid,id);
        res.setSuccess(true);
        res.setMsg("打印成功！");
        return res;
    }

    /**
     * 盘点车辆数/自动处理盘点结果
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/countInventoryCar")
    @ResponseBody
    public ResultEntity countInventoryCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token,
                                      @RequestParam(name = "id", defaultValue = "") String id)
            throws Exception {
        ResultEntity res = new ResultEntity();
        itScmInventoryDetailService.countInventoryCar(uid,id);
        res.setSuccess(true);
        res.setMsg("盘点车辆总数已计算");
        return res;
    }



}
